package com.silverbox.waimai.service.member.impl;

import com.github.pagehelper.PageHelper;
import com.silverbox.core.result.CoreResponse;
import com.silverbox.core.util.DeliveryPayUtils;
import com.silverbox.shopper.dubboApi.info.ShopperInfoServiceApi;
import com.silverbox.waimai.entity.ReceiverAddress;
import com.silverbox.waimai.mapper.ReceiverAddressMapper;
import com.silverbox.waimai.mapper.WaimaiMapper;
import com.silverbox.waimai.service.member.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <pre>
 * 功    能:
 * 涉及版本:
 * 创 建 者: YangYangjun
 * 日    期: 2020-03-07 10:37
 * </pre>
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private ReceiverAddressMapper receiverAddressMapper;
    @Autowired
    private WaimaiMapper waimaiMapper;
    @Autowired
    private ShopperInfoServiceApi shopperInfoServiceApi;

    @Override
    public CoreResponse<Object> addOrUpdateReceiverAddress(ReceiverAddress receiverAddress) {
        CoreResponse<Object> res = new CoreResponse<>();
        Integer id = receiverAddress.getId();
        //当设置默认地址的时候置空其他默认地址设置
        if(receiverAddress.getIsDefault() != null && receiverAddress.getIsDefault() == 1){
            receiverAddressMapper.updateDefaultOthers(receiverAddress.getId(),receiverAddress.getThirdUserId());
        }
        if(id != null){
            int count = receiverAddressMapper.updateByPrimaryKeySelective(receiverAddress);
            if(count < 1){
                res.setCode(CoreResponse.StatusInfo.FORBIDDEN.getCode());
                res.setSubMsg("地址信息不存在");
                return res;
            }
            res.setSubMsg("修改成功");
        }else{
            receiverAddress.setCreateTime(new Date());
            int count = receiverAddressMapper.insertSelective(receiverAddress);
            if(count < 1){
                res.setCode(CoreResponse.StatusInfo.FORBIDDEN.getCode());
                res.setSubMsg("服务器开小差,请稍后再试");
                return res;
            }
            res.setSubMsg("新增成功");
        }

        res.setMsg(CoreResponse.StatusInfo.SUCCESS.getMessage());
        return res;
    }

    @Override
    public CoreResponse<Object> deleteReceiverAddress(Integer addressId) {
        CoreResponse<Object> res = new CoreResponse<>();

        int count = receiverAddressMapper.deleteByPrimaryKey(addressId);
        if(count < 1){
            res.setCode(CoreResponse.StatusInfo.FORBIDDEN.getCode());
            res.setSubMsg("删除失败");
            return res;
        }
        res.setSubMsg("删除成功");
        res.setMsg(CoreResponse.StatusInfo.SUCCESS.getMessage());
        return res;
    }

    @Override
    public CoreResponse<Object> findReceiverAddressList(String buyerId, Integer pageNo, Integer pageSize,Double maxTakeOutDistance, Double shopLat, Double shopLng) {
        CoreResponse<Object> res = new CoreResponse<>();
        PageHelper.startPage(pageNo,pageSize);
        List<ReceiverAddress> receiverAddressList = receiverAddressMapper.findReceiverAddressList(buyerId);
        double distance;
        for (ReceiverAddress rd : receiverAddressList) {
            distance = DeliveryPayUtils.getDistance(Double.valueOf(rd.getReceiverLng()), Double.valueOf(rd.getReceiverLat()), shopLng, shopLat);
            if(distance > maxTakeOutDistance){
                rd.setFlag(0);
            }else {
                rd.setFlag(1);
            }
        }
        int count = receiverAddressMapper.countReceiverAddressList(buyerId);
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("list",receiverAddressList);
        resultData.put("count",count);
        res.setSubMsg("查询成功");
        res.setMsg(CoreResponse.StatusInfo.SUCCESS.getMessage());
        res.setData(resultData);
        return res;
    }

}
